package com.sxyjhh.dbutil.dao;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.sxyjhh.dbutil.basic.ConstConfig;
import com.sxyjhh.dbutil.basic.CustomDataSource;
import com.sxyjhh.dbutil.utils.DBConstConfig;
import com.sxyjhh.dbutil.utils.DBInfoUtil;
import com.sxyjhh.dbutil.utils.ResourceLoader;
import lombok.extern.log4j.Log4j;

/**
 * 基础dao控制
 * Created by wangchun on 2016/4/11.
 */
@Log4j
public class LocalMongodbDataSource {

    private static LocalMongodbDataSource instance;
    private static MongoDatabase customDataSource;

    private LocalMongodbDataSource() {
    }

    public synchronized static LocalMongodbDataSource getInstance() {
        if (instance == null) {
            instance = new LocalMongodbDataSource();
            ResourceLoader resourceLoader = new ResourceLoader(DBConstConfig.DDConfigFileName.DB_CONFIG);

/*            customDataSource = new CustomDataSource(DBInfoUtil.getDBDriverName(ConstConfig.DBType.MYSQL),
                    DBInfoUtil.getDBUrl(ConstConfig.DBType.MYSQL,
                            resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_IP),
                            resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_PORT),
                            resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_NAME)),
                    resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_USER_NAME),
                    resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_PASSWD),
                    poolInitialSize == null ? 0 : Integer.valueOf(poolInitialSize));*/

            MongoClient mongoClient = new MongoClient(resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_IP),
                    Integer.parseInt(resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_PORT)));
            customDataSource = mongoClient.getDatabase(resourceLoader.getPropertiesValue(DBConstConfig.DDConfigPropertiesKey.INFO_DB_NAME));
        }
        return instance;
    }

    public MongoDatabase getMongoDatabase() {
        return customDataSource;
    }

}
